/*
 * IconsDAO.java
 *
 * Created on April 7, 2007, 11:06 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.group1E.data;

import edu.byu.isys413.group1E.handlers.ConnectionPool;
import edu.byu.isys413.group1E.handlers.ConnectionPoolException;
import edu.byu.isys413.group1E.handlers.DataException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/** This class handles interaction with the Icons table in the DB.  It does not contain 
 *the standard CRUD methods for the purposes of the project.  If those are needed at some other 
 *point, they would be very similar to any other DAO.
 *
 * @author Travis Kulbeth
 */
public class IconsDAO {
    
    /** Contains an instance of IconsDAO*/
    private static IconsDAO instance = null;
    
    /** Creates a new instance of IconsDAO */
    public IconsDAO() {
    }
    
    /**Retrieves an instance of the TransactionDAO object
     *@return instance of IconsDAO
     */
    public static synchronized IconsDAO getInstance() {
        if (instance == null) {
            instance = new IconsDAO();
        }
        return instance;
    }
    
    /**
     * This is a public read statement.  It returns an existing Icon name by the file extension record
     * from the database based on the ext and returns a String.
     * 
     * @return a BackupBO containing the information extracted from the requested
     * tuple in the DB.
     * @param ext contains the extension for the file
     * @throws edu.byu.isys413.group1E.handlers.DataException handles any errors from data
     * @throws java.sql.SQLException catches any errors from the SQL execution
     * @throws edu.byu.isys413.group1E.handlers.ConnectionPoolException catches any errors with the connection
     */
     public String readByExt(String ext) throws SQLException, DataException, ConnectionPoolException {
            Connection conn = ConnectionPool.getInstance().get();             // retrieve a database connection from the pool
            PreparedStatement pstmt = conn.prepareStatement("SELECT iconFileName FROM Icons WHERE ext = ?");
            pstmt.setString(1,ext);
            ResultSet rs = pstmt.executeQuery();
            
            String filename = null;
            while(rs.next()){
                filename = rs.getString("iconFileName");
            }
            ConnectionPool.getInstance().release(conn);
            return filename;
     }
     
    /**
     * This is a public read statement.  It returns an existing Icon name by the file type
     * from the database based on the file type extracted from a file's metadata and returns a String.
     * 
     * @return a BackupBO containing the information extracted from the requested
     * tuple in the DB.
     * @param ftype contains the file type
     * @throws edu.byu.isys413.group1E.handlers.DataException handles any errors from data
     * @throws java.sql.SQLException catches any errors from the SQL execution
     * @throws edu.byu.isys413.group1E.handlers.ConnectionPoolException catches any errors with the connection
     */
     public String readByFileType(String ftype) throws SQLException, DataException, ConnectionPoolException {
            Connection conn = ConnectionPool.getInstance().get();             // retrieve a database connection from the pool
            PreparedStatement pstmt = conn.prepareStatement("SELECT iconFileName FROM Icons WHERE fileType = ?");
            pstmt.setString(1,ftype);
            ResultSet rs = pstmt.executeQuery();
            
            String filename = null;
            while(rs.next()){
                filename = rs.getString("iconFileName");
            }
            ConnectionPool.getInstance().release(conn);
            return filename;
     }     
}
